package com.bosssoft.g1.controller;
import cn.hutool.core.util.ObjectUtil;
import com.bosssoft.g1.common.data.vo.CommonResponse;
import com.bosssoft.g1.common.util.web.JWTUtils;
import com.bosssoft.g1.config.ContextHolder;
import com.bosssoft.g1.dto.ResourceDTO;
import com.bosssoft.g1.dto.UserDTO;
import com.bosssoft.g1.entity.User;
import com.bosssoft.g1.api.LoginApi;
import com.bosssoft.g1.mapper.ResourceMapper;
import com.bosssoft.g1.query.UserAuthQuery;
import com.bosssoft.g1.query.UserLoginQuery;
import com.bosssoft.g1.service.LoginService;
import com.bosssoft.g1.service.UserService;
import com.bosssoft.g1.service.impl.ResourceServiceImpl;
import com.bosssoft.g1.utils.CommonResponseUtils;
import com.bosssoft.g1.utils.JwtUtil;
import com.bosssoft.g1.vo.ResourceMenuVO;
import com.bosssoft.g1.vo.ResourceVO;
import com.bosssoft.g1.vo.UserAuthVO;
import com.bosssoft.g1.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * @author WangXin
 */
@RestController
@Slf4j
public class LoginController implements LoginApi {

    private final UserService userService;
    private final LoginService loginService;

    @Autowired
    private ContextHolder contextHolder;
    @Autowired
    private ResourceMapper resourceMapper;
    @Autowired
    private ResourceServiceImpl resourceServiceImpl;

    // 构造器注入UserService
    @Autowired
    public LoginController(UserService userService, LoginService loginService) {
        this.userService = userService;
        this.loginService = loginService;
    }


//    @Autowired
//    private UserRoleService userRoleService;

    /**
     * 提供登录的认证功能
     *
     * @param userAuthQuery
     * @return true成功 false失败
     */
    @CrossOrigin
    @Override
    public CommonResponse<String> login(@NotNull @Valid @RequestBody UserLoginQuery userAuthQuery) {
        System.out.println(userAuthQuery);
        // 验证
        UserAuthVO userVO = userService.authenticate(userAuthQuery);
        if (ObjectUtil.isNull(userVO)) {
            // 用户认证失败
            return CommonResponse.<String>builder().withCode("-1").withMessage("用户名或密码错误").build();
        }

        // 如果认证成功
        String token = JwtUtil.generateToken(userVO);
        System.out.println(token);
        System.out.println(JwtUtil.parseToken(token));
        // 存储在ThreadLocal中
        contextHolder.setUserCode(userVO.getCode(), token);

        return CommonResponse.<String>builder().withCode("0").withMessage("登录成功").withBody(token).build();
    }

    /**
     * 提供获取角色资源的功能
     *
     * @param id 记录id
     * @return true 成功 false失败
     */
    @Override
    public CommonResponse<Boolean> roleResource(Long id) {
        // 查询用户下所有角色关系

        return null;
    }

    /**
     * 根据用户id显示对应子系统信息——动态路由1
     * @param id
     * @return
     */
    @Override
    public CommonResponse<List<Map<String, Object>>> queryMenuByRoleId(@NotNull @Valid Long id){
        List<Map<String, Object>> menus =  loginService.getMenu(id);
        return CommonResponseUtils.success(menus);
    }

    /**
     * 根据子系统id显示对应菜单资源信息——动态路由2
     * @param id
     * @return
     */
    @Override
    public CommonResponse<List<ResourceMenuVO>> queryMenuBySubsId(@NotNull @Valid Long id) {
        List<ResourceDTO> resourceDTOs = loginService.getMenuBySubId(id);
        if(resourceDTOs==null){
            return CommonResponseUtils.failed("resourceDTOs为空") ;
        }
        List<ResourceMenuVO> resourceVOs = new ArrayList<>();

        for (ResourceDTO resourceDTO : resourceDTOs) {
            ResourceMenuVO resourceVO = convertToResourceVO(resourceDTO);
            // 将转换后的对象添加到列表中
            resourceVOs.add(resourceVO);
        }

        return CommonResponseUtils.success(resourceVOs);
    }


    private ResourceMenuVO convertToResourceVO(ResourceDTO resourceDTO) {
        ResourceMenuVO resourceVO = new ResourceMenuVO();
        resourceVO.setId(resourceDTO.getId());
        resourceVO.setName(resourceDTO.getName());
        resourceVO.setUrl(resourceDTO.getUrl());
        return resourceVO;
    }
}